package com.example.demo.admin.controller;

import com.example.demo.admin.bean.AdminBean;
import com.example.demo.admin.service.AdminService;
import com.example.demo.controller.BaseController;
import com.example.demo.security.bean.SecurityRoleBean;
import com.example.demo.security.service.SecurityRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("Admin/admin")
public class AdminController extends BaseController<AdminService,AdminBean>{

    @Autowired
    private SecurityRoleService roleService;



    public AdminController() {
        rootName = "/admin/";
    }


    @Override
    public String addNewEntity(Model model) {
        model.addAttribute("roles",roleService.getAll());
       return super.addNewEntity(model);
    }

    @Override
    public String editEntity(@PathVariable int id, Model model) {
        model.addAttribute("roles",roleService.getAll());
        model.addAttribute("selectrole",((AdminService)service).getRole(id));
        return super.editEntity(id, model);
    }

    @Override
    @RequestMapping(value = "save",method = RequestMethod.GET)
    public String saveEntity(AdminBean entity, BindingResult result) {
        return rootName+"add";
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String saveEntity(AdminBean entity, BindingResult result,String[] roleBeanSet) {
        if (result.hasErrors())
        {
            return rootName+"add";
        }
        service.save(entity,roleBeanSet);
        return "redirect:"+DefRequestName+rootName+"list";
    }
}
